<template>
  <view class="climber-pwd">
    <!-- 头部导航 -->
    <view class="header">
      <view class="nav-bar">
        <view class="nav-left" @tap="goBack">
          <text class="back-icon">‹</text>
        </view>
        <text class="nav-title">修改密码</text>
        <view class="nav-right"></view>
      </view>
    </view>

    <!-- 表单内容 -->
    <view class="form-section">
      <view class="form-item">
        <text class="form-label">原密码</text>
        <input 
          class="form-input" 
          type="password" 
          v-model="form.oldPassword" 
          placeholder="请输入原密码"
          maxlength="20"
        />
      </view>

      <view class="form-item">
        <text class="form-label">新密码</text>
        <input 
          class="form-input" 
          type="password" 
          v-model="form.newPassword" 
          placeholder="请输入新密码"
          maxlength="20"
        />
      </view>

      <view class="form-item">
        <text class="form-label">确认密码</text>
        <input 
          class="form-input" 
          type="password" 
          v-model="form.confirmPassword" 
          placeholder="请再次输入新密码"
          maxlength="20"
        />
      </view>

      <view class="form-tips">
        <text class="tip-text">密码长度6-20位，建议包含字母和数字</text>
      </view>

      <button class="submit-btn" @tap="handleSubmit" :disabled="!canSubmit">
        确认修改
      </button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      form: {
        oldPassword: '',
        newPassword: '',
        confirmPassword: ''
      }
    }
  },
  computed: {
    canSubmit() {
      return this.form.oldPassword && 
             this.form.newPassword && 
             this.form.confirmPassword &&
             this.form.newPassword === this.form.confirmPassword
    }
  },
  methods: {
    goBack() {
      uni.navigateBack()
    },
    async handleSubmit() {
      if (!this.validateForm()) {
        return
      }

      try {
        uni.showLoading({ title: '修改中...' })
        
        // TODO: 调用修改密码接口
        // const res = await changePassword(this.form)
        
        // 模拟接口调用
        await new Promise(resolve => setTimeout(resolve, 1000))
        
        uni.hideLoading()
        uni.showModal({
          title: '修改成功',
          content: '密码修改成功，请重新登录',
          showCancel: false,
          success: () => {
            this.$store.dispatch('LogOut').then(() => {
              uni.reLaunch({ url: '/pages/login' })
            })
          }
        })
      } catch (error) {
        uni.hideLoading()
        uni.showToast({
          title: '修改失败',
          icon: 'error'
        })
      }
    },
    validateForm() {
      if (!this.form.oldPassword) {
        uni.showToast({ title: '请输入原密码', icon: 'none' })
        return false
      }
      
      if (!this.form.newPassword) {
        uni.showToast({ title: '请输入新密码', icon: 'none' })
        return false
      }
      
      if (this.form.newPassword.length < 6) {
        uni.showToast({ title: '密码长度不能少于6位', icon: 'none' })
        return false
      }
      
      if (this.form.newPassword !== this.form.confirmPassword) {
        uni.showToast({ title: '两次输入的密码不一致', icon: 'none' })
        return false
      }
      
      return true
    }
  }
}
</script>

<style scoped>
.climber-pwd {
  min-height: 100vh;
  background: #f5f6f7;
}

/* 头部导航 */
.header {
  background: #fff;
  padding-top: var(--status-bar-height);
}

.nav-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 88rpx;
  padding: 0 32rpx;
}

.nav-left {
  width: 80rpx;
}

.back-icon {
  font-size: 48rpx;
  color: #333;
  font-weight: 300;
}

.nav-title {
  flex: 1;
  text-align: center;
  font-size: 36rpx;
  font-weight: 600;
  color: #333;
}

.nav-right {
  width: 80rpx;
}

/* 表单内容 */
.form-section {
  background: #fff;
  margin: 20rpx 32rpx;
  border-radius: 16rpx;
  padding: 32rpx;
}

.form-item {
  margin-bottom: 32rpx;
}

.form-item:last-of-type {
  margin-bottom: 0;
}

.form-label {
  display: block;
  font-size: 28rpx;
  color: #333;
  font-weight: 500;
  margin-bottom: 16rpx;
}

.form-input {
  width: 100%;
  height: 80rpx;
  background: #f5f6f7;
  border-radius: 12rpx;
  padding: 0 24rpx;
  font-size: 28rpx;
  color: #333;
  border: 1rpx solid transparent;
}

.form-input:focus {
  border-color: #1989fa;
  background: #fff;
}

.form-tips {
  margin: 24rpx 0 40rpx;
}

.tip-text {
  font-size: 24rpx;
  color: #999;
}

.submit-btn {
  width: 100%;
  height: 88rpx;
  background: #1989fa;
  color: #fff;
  border: none;
  border-radius: 16rpx;
  font-size: 32rpx;
  font-weight: 600;
}

.submit-btn[disabled] {
  background: #ccc;
  color: #999;
}
</style>
